package cn.zh.npro.Adapter;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.zh.npro.Item.AcLItem;
import cn.zh.npro.R;

/**
 * Created by WYN on 2018/3/14 0014.
 */

public class AcLadapter extends BaseAdapter{
    Context context;
    ArrayList<AcLItem> arrayList;
    LayoutInflater inflater;
    Dialog dialog;
    public  AcLadapter(Context context,ArrayList<AcLItem>arrayList){
        this.context=context;
        this.arrayList=arrayList;
        inflater=LayoutInflater.from(context);
        }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
      Holder holder=null;
        if (null==view){
            holder=new Holder();
            view=inflater.inflate(R.layout.ca_layut,null,false);
            holder.cpname=view.findViewById(R.id.cpname);
            holder.cpmobile=view.findViewById(R.id.cpmobile);
            holder.dename=view.findViewById(R.id.dename);
            holder.cpdress=view.findViewById(R.id.cpdress);
            holder.ratxt=view.findViewById(R.id.ratxt);
            holder.ratxt.setTextColor(context.getResources().getColor(R.color.text_select_color));
            holder.cpedit=view.findViewById(R.id.cpedit);
            holder.cpdete=view.findViewById(R.id.cpdete);
            holder.radio=view.findViewById(R.id.radio);
            view.setTag(holder);
        }else {
            holder= (Holder) view.getTag();
        }
        holder.cpname.setText(arrayList.get(i).getFullname());
        holder.cpmobile.setText(arrayList.get(i).getMobile());
        holder.cpdress.setText(arrayList.get(i).getAddress());
        if (arrayList.get(i).isMaster()){
            holder.radio.setBackgroundResource(R.mipmap.round_check_fill);
            holder.ratxt.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        }else {
            holder.radio.setBackgroundResource(R.mipmap.round);
            holder.ratxt.setTextColor(context.getResources().getColor(R.color.text_select_color));
        }

        holder.dename.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (arrayList.get(i).isMaster()){
                }else {
                    //设置默认联系人
                    for (int j=0;j<arrayList.size();j++){
                        if (j==i){
                            arrayList.get(j).setMaster(true);
                        }else {
                            arrayList.get(j).setMaster(false);
                         }
                       }
                    notifyDataSetChanged();
                  }
                }
           });

        holder.cpedit.setVisibility(View.GONE);
//        holder.cpedit.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent=new Intent(context,EditClient.class);
//                intent.putExtra("ob",arrayList.get(i));
//                context.startActivity(intent);
//              }
//        });

        holder.cpdete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             getDialog(i);
            }
        });

        return view;

    }


    public  class Holder{
        TextView cpname,cpmobile,cpdress,ratxt,cpedit,cpdete;
        ImageView radio;
        LinearLayout dename;
       }



    public void getDialog(final int i){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.out_layout, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.out_layout);// 加载布局
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
               arrayList.remove(i);
               notifyDataSetChanged();
            }
        });
        dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("是否删除此联系人?");
     }


}
